package com.y.mytest.jm;

import com.y.mytest.jm.aes.AESUtils;
import okhttp3.*;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Objects;
import java.util.zip.Deflater;
import java.util.zip.InflaterOutputStream;

/**
 * @ClassName: testrm
 * @author: 叶子豪
 * @create: 2021-04-12 11:38
 */
public class testrm {
    public static void main(String[] args) throws Exception {
        String data = "{ \"qr_token\": \"123456\", " +
                "    \"qr_validity_start\": \"2021-12-11T15:30:00Z\",\n" +
                "    \"qr_validity_end\": \"2021-03-31T17:31:18+01:00\",\n" +
                "    \"qr_validity_serials\": [ \"12321SS49H4L\",\"12321SS2EDGG\"],\n" +
                "    \"qr_areas\": [1, 2] }";

        byte[] compressData = compress(data.getBytes());
        String zlib = new String(compressData, StandardCharsets.ISO_8859_1);
        //System.out.println("y压缩后的字节流：" + Arrays.toString(compressData));
        System.out.println("压缩后的字符串：" + zlib);
        System.out.println("压缩后的字节流：" + Arrays.toString(zlib.getBytes(StandardCharsets.ISO_8859_1)));

        byte[] encrypt2 = AESUtils.encrypt2(compressData);
        String encryptStr = new String(encrypt2, StandardCharsets.ISO_8859_1);
        System.out.println("y加密的字节流：" + Arrays.toString(encrypt2));
        System.out.println(encryptStr);

        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("text/plain");
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("encryptionJson", encryptStr)
                .build();
        Request request = new Request.Builder()
                .url("http://94.177.203.68:6064/survey/cloudServer/getDeviceQRCodeKey2")//94.177.203.68
                .method("POST", body)
                .addHeader("Cookie", "JSESSIONID=d2d50d60-dde4-421c-94ad-236073fdd25e")
                .build();
        Response response = client.newCall(request).execute();
        System.out.println(response);
        System.out.println(Objects.requireNonNull(response.body()).string());
        /*byte[] bytes1 = Objects.requireNonNull(response.body()).bytes();
        byte[] bytes = AESUtils.decrypt2(bytes1);
        String decrypt = new String(bytes, StandardCharsets.ISO_8859_1);
        System.out.println("解密后：" + decrypt);

        System.out.println("解压：" + decompress(decrypt));*/

    }

    public static byte[] compress(byte[] data) {
        byte[] output = new byte[0];

        Deflater compresser = new Deflater();

        compresser.reset();
        compresser.setInput(data);
        compresser.finish();
        ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
        try {
            byte[] buf = new byte[1024];
            while (!compresser.finished()) {
                int i = compresser.deflate(buf);
                bos.write(buf, 0, i);
            }
            output = bos.toByteArray();
        } catch (Exception e) {
            output = data;
            e.printStackTrace();
        } finally {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        compresser.end();
        return output;
    }


    public static String decompress(String compressedTxt) {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try (OutputStream ios = new InflaterOutputStream(os)) {
            ios.write(compressedTxt.getBytes(StandardCharsets.ISO_8859_1));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return os.toString();
    }

    public static byte[] decompress(byte[] compressedTxt) {

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try (OutputStream ios = new InflaterOutputStream(os)) {
            ios.write(compressedTxt);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return os.toByteArray();

    }
}